package com.okaixz.client.controller;


import com.okaixz.client.model.dto.UpdateUserDto;
import com.okaixz.client.model.dto.UserDto;
import com.okaixz.client.model.vo.CommonInfoVo;
import com.okaixz.client.service.ITokenService;
import com.okaixz.client.service.IUserService;
import com.okaixz.common.annotation.OpLog;
import com.okaixz.common.client.Result;
import com.okaixz.common.constant.OpConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * APP用户信息表 前端控制器
 * </p>
 *
 * @author cdz
 * @since 2024-10-08
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private ITokenService tokenService;

    @PostMapping("/update/flag")
    public Result<Integer> updateFlag(@RequestParam Integer flag){
        Integer result=userService.updateFlag(flag);
        return Result.success(result);
    }
    @PostMapping("/update")
    @OpLog(opType = "", opDesc = OpConstants.USER_UPDATE,isSaveResponseData =false)
    public Result<UserDto> updateUser(@RequestBody UpdateUserDto updateUserDto){
        UserDto result=userService.updateUserInfoByUserId(updateUserDto);
        if (result!=null) {
            return Result.success(result);
        }
        return Result.error("服务异常");
    }

    @PostMapping("/kb/updatesex")
    @OpLog(opType = "", opDesc = "更新性别")
    public Result<UserDto> kbUpdateUserSex(@RequestBody UpdateUserDto updateUserDto){
        UserDto result=userService.kbUpdateUserSex(updateUserDto);
        if (result!=null) {
            return Result.success(result);
        }
        return Result.error("服务异常");
    }
    @PostMapping("/kb/pwd/update")
    @OpLog(opType = "", opDesc = "更新密码")
    public Result<Boolean> updatePwd(@RequestParam String pwdNew,@RequestParam String pwdOld,@RequestParam String userId){
        Boolean result=userService.updatePwd(pwdNew,pwdOld,userId);
        return Result.success(result);
    }
    @PostMapping("/kb/pwd/check")
    @OpLog(opType = "", opDesc = "校验密码")
    public Result<Integer> checkPwd(@RequestParam String pwd){
        Integer result=userService.checkPwd(pwd);
        return Result.success(result);
    }
    @PostMapping("/head")
    public Result<UserDto> saveHead(@RequestBody UserDto userDto){
        UserDto user=userService.saveHead(userDto);
        if (user!=null) {
            return Result.success(user);
        }
        return Result.error("服务异常");
    }
    @GetMapping("/{userId}")
    @OpLog(opType = "", opDesc = OpConstants.USER_GET_INFO,isSaveResponseData =false)
    public Result<UserDto> getUserById(@PathVariable(name = "userId",required = true) String userId){
        UserDto user = tokenService.refreshUserAndToken(userId);
        if (user!=null) {
            return Result.success(user);
        }
        return Result.error("服务异常");
    }
    @GetMapping("/logoff/{userId}")
    @OpLog(opType = "", opDesc = OpConstants.USER_LOGOFF,isSaveResponseData =true)
    public Result<Boolean> logoff(@PathVariable(name = "userId",required = true) String userId){
        boolean result=userService.logoffByUserId(userId);
        return Result.success(result);
    }



}
